//
// Created by h00422988 on 2021/8/20.
//

#ifndef DEMO_AUDIO_TRACK_PROXY_H
#define DEMO_AUDIO_TRACK_PROXY_H
#ifdef __ANDROID__
#include <jni.h>
#include <memory>
#include <vector>
#include "plugin/common/plugin_audio_tags.h"

#ifdef __cplusplus
extern "C" {
#endif
#include "libswresample/swresample.h"
#include "libswscale/swscale.h"
#ifdef __cplusplus
};
#endif

namespace OHOS {
namespace Media {
namespace Plugin {
class AudioTrackProxy {
public:
    AudioTrackProxy();
    ~AudioTrackProxy();

    bool Open(int32_t sampleRate, AudioSampleFormat sampleFormat, int32_t channels,
              uint64_t channelMask, int32_t samples);

    bool Play();
    bool Pause();
    bool Stop();
    bool Flush();

    bool Write(uint8_t* data, size_t len);

    bool Release();

private:
    jobject audioTrack_;
    jmethodID play_;
    jmethodID pause_;
    jmethodID stop_;
    jmethodID flush_;
    jmethodID write_;
    jmethodID release_;
    jmethodID getState_;
    jmethodID getPlayState_;
    jmethodID positionBuffer_;
    std::shared_ptr<SwrContext> swrCtx_{nullptr};
    jobject bufferCache_;
    std::unique_ptr<uint8_t[]> transformCache_;
    size_t transformSize_;
    int32_t channels_;
    AudioSampleFormat format_;
    int32_t samplesPerFrame_;
    int32_t avFrameSize_;
};
}
}
}
#endif
#endif //DEMO_AUDIO_TRACK_PROXY_H
